package com.itheima.web;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.PageParam;
import com.itheima.dto.DishDto;
import com.itheima.common.R;
import com.itheima.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/dish")
@RestController
public class DishController {

    @Autowired
    private DishService dishService;

    //菜品管理——新增菜品
    @PostMapping
    public R add(@RequestBody DishDto dishDto){
        boolean result = dishService.add(dishDto);
        if(result){
            return  R.success("添加成功");
        }
        return  R.error("添加失败");
    }

    //菜品管理——菜品分页
    @GetMapping("/page")
    public R page(PageParam pageParam){
        //1.调用service
        IPage<DishDto> page = dishService.page(pageParam);

        //2.返回结果
        return R.success(page);
    }

    //根据ID查找菜品信息和口味信息（回显）
    @GetMapping("/{id}")
    public R findById(@PathVariable long id){
        //1.调用service层
        DishDto dto = dishService.findById(id);
        //2.返回结果
        return R.success(dto);
    }

    //修改更新菜品信息
    @PutMapping
    public R update(@RequestBody DishDto dishDto){
        boolean result = dishService.update(dishDto);

        if(result){
            return R.success("更新成功");
        }
        return R.error("更新失败");
    }

    //根据分类id查询菜品数据
    @GetMapping("/list")
    public R findByCategoryId(long categoryId, Integer status){

        List<DishDto> dishList = dishService.findByCid(categoryId,status);

        return R.success(dishList);
    }

}
